//
//  AlertManager.h
//  Transfar_Business
//
//  Created by RiceJia on 15/11/11.
//  Copyright © 2015年 Transfar. All rights reserved.
//
#import "AlertManager.h"
#import <objc/runtime.h>
#define DEVICE_VESION          ([[[UIDevice currentDevice] systemVersion] floatValue])
#define VERSION(sign ,index) (DEVICE_VESION sign index)
#define iOS8Later VERSION(>= , 8.0)
@implementation AlertManager
- (void)showWithTitle:(NSString *)title message:(NSString *)message sure:(NSString *)sureTitle negate:(NSString *)negateTitle on:(UIViewController *)viewController sureBlock:(SureBlock)sureBlock negateBlock:(NegateBlock)negateBlock
{
    [self showWithTitle:title message:message msgAlign:CHYBAlertMsgAlignDefault  sure:sureTitle negate:negateTitle on:viewController sureBlock:sureBlock negateBlock:negateBlock];
}

- (void)showWithTitle:(NSString *)title message:(NSString *)message msgAlign:(CHYBAlertMsgAlign)msgAlign sure:(NSString *)sureText negate:(NSString *)negateText on:(UIViewController *)viewController sureBlock:(SureBlock)sureBlock negateBlock:(NegateBlock)negateBlock {
    if (iOS8Later)
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        if (msgAlign != CHYBAlertMsgAlignDefault) {
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.alignment = (NSTextAlignment)msgAlign;
            NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSParagraphStyleAttributeName: style}];
            [alert setValue:string forKey:@"attributedMessage"];
        }
        
        UIAlertAction * negateAction = [UIAlertAction actionWithTitle:negateText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            if (negateBlock) {
                negateBlock();
            }
        }];
        [alert  addAction:negateAction];
        
        UIAlertAction * sureAction = [UIAlertAction actionWithTitle:sureText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            if (sureBlock) {
                sureBlock();
            }
        }];
        [alert  addAction:sureAction];
        [viewController presentViewController:alert animated:YES completion:nil];
    }else{
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:negateText otherButtonTitles:sureText, nil];
        alert.delegate = alert;
        if (msgAlign != CHYBAlertMsgAlignDefault) {
            NSArray *subview = [alert subviews];
            UILabel *label = subview[2];
            label.textAlignment = (NSTextAlignment)msgAlign;
        }
        [alert show];
        alert.sureBlock = sureBlock;
        alert.negateBlock = negateBlock;
    }
}

- (void)showWithTitle:(NSString *)title message:(NSString *)message sure:(NSString *)sureText on:(UIViewController *)viewController sureBlock:(SureBlock)sureBlock
{
    if (iOS8Later)
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction * sureAction = [UIAlertAction actionWithTitle:sureText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            if (sureBlock) {
                sureBlock();
            }
        }];
        [alert  addAction:sureAction];
        [viewController presentViewController:alert animated:YES completion:nil];
    }else{
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:sureText, nil];
        alert.delegate = alert;
        [alert show];
        alert.sureBlock = sureBlock;
    }
}
- (void)showWithTitle:(NSString *)title message:(NSString *)message msgAlign:(CHYBAlertMsgAlign)msgAlign sure:(NSString *)sureText on:(UIViewController *)viewController sureBlock:(SureBlock)sureBlock{
    if (iOS8Later)
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = (NSTextAlignment)msgAlign;
        NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSParagraphStyleAttributeName: style}];
        [alert setValue:string forKey:@"attributedMessage"];
        UIAlertAction * sureAction = [UIAlertAction actionWithTitle:sureText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            if (sureBlock) {
                sureBlock();
            }
        }];
        [alert  addAction:sureAction];
        [viewController presentViewController:alert animated:YES completion:nil];
    }else{
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:sureText, nil];
        alert.delegate = alert;
        NSArray *subview = [alert subviews];
        UILabel *label = subview[2];
        label.textAlignment = (NSTextAlignment)msgAlign;
        [alert show];
        alert.sureBlock = sureBlock;
    }
}

@end

/*iOS8以下，使用扩展UIAlaertView属性*/
@implementation UIAlertView (block)

#pragma getter
- (SureBlock)sureBlock {
    return objc_getAssociatedObject(self, "sureBlock");
}

- (NegateBlock)negateBlock {
    return objc_getAssociatedObject(self, "negateBlock");
}

#pragma setter
- (void)setSureBlock:(void (^)())sureBlock {
    objc_setAssociatedObject(self,"sureBlock",sureBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setNegateBlock:(void (^)())negateBlock {
    objc_setAssociatedObject(self, "negateBlock", negateBlock, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark- UIAlerViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        if (alertView.negateBlock) {
            alertView.negateBlock();
        }else{
            alertView.sureBlock();
        }
    }else if (buttonIndex == 1){
        alertView.sureBlock();
    }
}

@end
